import java.io.*;
import java.util.*;
import javax.mail.*;  

public class ReadMail 
{  
    public static String printDate(String m, String d, String y)
    {
        String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

        int month = 0;

        for( int i = 0; i < 12; i++ )
        {
            if(months[i].equalsIgnoreCase(m))
                month = i + 1;
        }

        String mon = Integer.toString(month);

        if( month < 10)
        {
            mon = "0" + mon;
        }
        return mon + "/" + d + "/" + y;
    }

    public static void main(String args[]) throws Exception 
    {  

        String host = "pop.gmail.com";
        final String username = "danielmuckerman@gmail.com";
        final String password = "tiZ80ZILog";  

        Authenticator auth=new Authenticator()
            {

                protected PasswordAuthentication getPasswordAuthentication()
                {
                    return new PasswordAuthentication(username, password); 
                }

            };
        Properties props=System.getProperties(); 

        Session session = Session.getInstance(props,auth);
        session.setDebug(false); 
        Store store = session.getStore("imaps");

        System.out.println("Connecting to gmail...");
        store.connect(host, username, password);
        System.out.println("Connected!");
        Folder inbox = store.getDefaultFolder().getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);

        Message[] msg = inbox.getMessages();

        // Display message.
        for (int i = 0; i < msg.length; i++) 
        {
            String date = msg[i].getSentDate().toString();

            System.out.println("------------ Message " + (i + 1) + " ------------");

            System.out.println("Date Received : " + printDate(date.substring(4,7), date.substring(8,10), date.substring(26)));
            System.out.println("From : " + msg[i].getFrom()[0]);
            System.out.println("Subject : " + msg[i].getSubject());
            System.out.print("Message : ");

            //             InputStream stream = msg[i].getInputStream();
            //             while (stream.available() != 0) {
            //                 System.out.print((char) stream.read());
            //             }
            System.out.println();
        }

        String date = msg[0].getSentDate().toString();

        inbox.close(true);
        store.close();
    }
}